﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Abp.AutoMapper;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using MyCompanyName.AbpZeroTemplate.Prm.Admin;

namespace MyCompanyName.AbpZeroTemplate.Prm.Synth
{
    public class SynthesisAppService : AbpZeroTemplateAppServiceBase, ISynthesisAppService
    {
        private readonly IRepository<Synthesis, int> synthesisRepository;
        private readonly OperationLogManager operationLogManager;

        public SynthesisAppService(IRepository<Synthesis, int> _synthesisRepository
            , OperationLogManager _operationLogManager)
        {
            synthesisRepository = _synthesisRepository;
            operationLogManager = _operationLogManager;
        }

        public async Task CreateOrEdit(Dto.CreateOrEditInPut input)
        {
            if (input.Id == 0)
            {
                var model = input.MapTo<Synthesis>();
                await synthesisRepository.InsertAsync(model);


            }
            else
            {
                var synth = await synthesisRepository.GetAsync(input.Id);
                synth.CDX = input.CDX;
                synth.ImageBase = input.ImageBase;
                synth.Remark = input.Remark;
                synth.SynthName = input.SynthName;
            }

            await operationLogManager.Create(input.ProjectId, "项目工艺路线编辑", HostOperation.项目, "");

        }


        public List<Dto.GetSynthesisOutPut> GetSynthesis(int projectId)
        {
            var alls = synthesisRepository.GetAll().Where(x => x.ProjectId == projectId).ToList();
            var results = alls.MapTo<List<Dto.GetSynthesisOutPut>>();
            return results;
        }


        public async Task<Dto.GetSynthesisOutPut> Get(int synthId)
        {
            var outPut = await synthesisRepository.GetAsync(synthId);
            var model = outPut.MapTo<Dto.GetSynthesisOutPut>();
            return model;
        }


        public async Task Remove(int synthId)
        {
            var synth = synthesisRepository.Get(synthId);
            await operationLogManager.Create(synth.ProjectId, "项目工艺路线删除", HostOperation.项目, "");

            await synthesisRepository.DeleteAsync(synthId);
        }
    }
}
